import axios from 'axios'
/* 引入nprogress */
// import NProgress from "nprogress";
import { Message } from 'element-ui'
// import "nprogress/nprogress.css";
import { httpUrl } from '@/config'
import { getToken } from '@/utils/auth'
axios.defaults.timeout = 15000
// Message.duration = 1000;
axios.interceptors.request.use(
  config => {
    config.baseURL = httpUrl
    if (!config.url.includes('/login')) {
      config.headers.common['Authorization'] = 'Bearer ' + getToken()
    }
    // NProgress.start();
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

axios.interceptors.response.use(
  function(response) {
    // 对响应数据做点什么
    const res = response.data
    console.log(res)
    return Promise.resolve(res)
  },
  function(err) {
    console.log(err, httpUrl)
    // NProgress.done();
    Message.error(err.message)
    return Promise.reject(err.response.data)
  }
)

export default axios
